<?php

namespace app\beautiful\model\goods;

use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\common\model\CommonModel;
use think\facade\Db;


class GoodsGroupDetailModel extends CommonModel{

    protected  $name = 'beautiful_goods_group_detail';

    protected  $pk   = 'goods_group_detail_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function up($data,$where){
        $res=self::update($data,$where);
        return $res;
    }
    public function goods()

    {

        return $this->hasOne(GoodsModel::class,'goods_id','goods_id');

    }
    public static function getById($where)

    {
        $info = self::where($where)->find();
        $group_detail_goods = GoodsModel::with(['unit'])
            ->where(['goods_id'=>$info['goods_id'],'is_delete'=>0,'shop_id'=>$info['shop_id']])
            ->find();
        $info['name'] = $group_detail_goods->name;
        $info['unit'] = $group_detail_goods->unit->group_detail_name;
        $info['original_price'] = $group_detail_goods->original_price;
        $info['sku_name'] = NULL;
        if($info['sku_id']){
            $sku_model = new GoodsSkuModel();
            $sku = $sku_model->getById(['sku_id'=>$info['sku_id'],'goods_id'=>$info['goods_id'],'is_delete'=>0]);
            $info['sku_name'] = $sku['name'];
        }
        return $info;

    }
    public static function list($where,$order,$page=0,$limit=0,$field='*'){
        $query = Db::name('beautiful_goods_group_detail')
            ->where(['is_delete'=>0])
            ->when(isset($where['goods_id']),function ($query) use ($where){
                $query->where(['goods_id'=>$where['goods_id']]);
            })
            ->when(isset($where['goods_group_id']),function ($query) use ($where){
                $query->where(['goods_group_id'=>$where['goods_group_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['shop_id'=>$where['shop_id']]);
            })
            ->field($field);

        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->field($field)
            ->select()
            ->toArray();

        if(!empty($list)){
            $sku_model = new GoodsSkuModel();
            foreach ($list as &$v){
                $group_detail_goods = GoodsModel::with(['unit'])
                    ->where(['goods_id'=>$v['goods_id'],'is_delete'=>0,'shop_id'=>$v['shop_id']])
                    ->find();
                $v['name'] = $group_detail_goods->name;
                $v['unit'] = $group_detail_goods->unit->group_detail_name;
                $v['original_price'] = $group_detail_goods->original_price;
                $v['sku_name'] = NULL;
                if($v['sku_id']){
                    $sku = $sku_model->getById(['sku_id'=>$v['sku_id'],'goods_id'=>$v['goods_id'],'is_delete'=>0]);
                    $v['sku_name'] = $sku['name'];
                }
            }

        }

        return compact('count','list');



    }
}

